package com.lanou3g.musicenjoy.fragment;


import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.eventbus.MyEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class Bankragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private View view;
    private List<Fragment> datas;
    private MyAdapter myAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_main, null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        myAdapter = new MyAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(myAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(0xff9999ff);
        tabLayout.setTabsFromPagerAdapter(myAdapter);
        init();

    }
    public void onEvent(MyEvent.Recommend recommend){
        if (recommend.getA()==1){
            viewPager.setCurrentItem(2);
        }
    }

    private void init() {
        datas = new ArrayList<Fragment>();
        datas.add(new RecommendationFragment());
        datas.add(new SingerFragment());
        datas.add(new MenuFragment());
        datas.add(new MVFragment());
        myAdapter.setData(datas);

    }

    //将堆栈中的fragment销毁掉
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (!getActivity().isDestroyed()) {

            for (Fragment fragment : datas) {
                getFragmentManager()
                        .beginTransaction()
                        .remove(fragment)
                        .commit();
            }
        }
    }

    public class MyAdapter extends FragmentPagerAdapter {
        private List<Fragment> datas;
        private String[] tytle = {getString(R.string.recommendation), getString(R.string.singer), getString(R.string.singermenu), getString(R.string.mv)};

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setData(List<Fragment> datas) {
            this.datas = datas;
            notifyDataSetChanged();
        }


        @Override
        public Fragment getItem(int position) {
            return datas.get(position);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tytle[position];
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
     EventBus.getDefault().unregister(this);
    }
}
